package com.lywz.web.interceptor;

import java.lang.reflect.Method;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.lywz.core.log.LogClerk;
import com.lywz.core.util.ActionUtil;
import com.lywz.core.web.PostCheck;
import com.lywz.core.web.WebConstants;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

/**
 * post方法检查
 * 
 * @author Lionel pang
 * @since 1.0
 * 
 */
public class PostCheckInterceptor extends AbstractInterceptor {

    private static final long serialVersionUID = -1226723788277160929L;

    @Override
    public String intercept(ActionInvocation inv) throws Exception {
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpServletResponse response = ServletActionContext.getResponse();
        // TODO 
        LogClerk.sysout.info("@@@@@@@ CFK Test:" + request.getServletPath());
        
        // 获取方法名
        String invoMethodName = inv.getProxy().getMethod();
        Object action = inv.getAction();
        Method method = action.getClass().getMethod(invoMethodName);

        PostCheck postChek = method.getAnnotation(PostCheck.class);
        // 该方法需要进行post方法检查
        if (postChek != null) {
            // 进行检查
            boolean result = ActionUtil.checkPost(request, response);
            if (!result) {
                // 不符合要求，返回null
                return WebConstants.forbidenCode;
            }
        }

        return inv.invoke();
    }
}
